﻿/**!
 * @file Jx.util.js
 * @author JerryLin.DK
 * @version v1.0 
 !*/

(function (window, undefined) {
    var Jx = window["Jx"] = window["Jx"] || {};

    var Linq = (function () {
        function Linq(data, func) {
            if (Object.prototype.toString.call(data) === '[object Array]') {
                this.type = "array";
            } else if (typeof data === "object") {
                this.type = "object";
            } else if (typeof data === "string" && data.length) {
                this.type = "string";
            } else if (typeof data === "number") {
                this.type = "number";
            }
            if (this.type) {
                this.data = data;
                func && this.each(func);
            }
        };

        Linq.prototype = {
            each: function (func) {
                var i; alert(this.type)
                if (!this.data || !func) { return this; }

                if (this.type == "array") {
                    for (i = 0; i < this.data.length;) {
                        if (func.call(this.data[i], i, this.data[i++]) === false) {
                            break;
                        }
                    }
                } else if (this.type == "object") {
                    for (i in this.data) {
                        if (func.call(this.data[i], i, this.data[i]) === false) {
                            break;
                        }
                    }
                } else if (this.type == "string") {
                    for (i = 0; i < this.data.length;) {
                        if (func.call(this.data.charAt(i), i, this.data.charAt(i++)) === false) {
                            break;
                        }
                    }
                } else if (this.type == "number") {
                    for (i = 1; i <= this.data;) {
                        if (func.call(i, i - 1, i++) === false) {
                            break;
                        }
                    }
                }
            }
        };

        return Linq;
    })();

    Jx.regexp = {
        Require: /.+/,
        Email: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
        Phone: /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/,
        Mobile: /^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/,
        Url: /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
        IdCard: /^\d{15}(\d{2}[A-Za-z0-9])?$/,
        Currency: /^\d+(\.\d+)?$/,
        Number: /^\d+$/,
        Zip: /^[1-9]\d{5}$/,
        QQ: /^[1-9]\d{4,8}$/,
        Integer: /^[-\+]?\d+$/,
        Double: /^[-\+]?\d+(\.\d+)?$/,
        English: /^[A-Za-z]+$/,
        Chinese: /^[\u0391-\uFFE5]+$/,
        UnSafe: /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
        IsSafe: function (str) { return !this.UnSafe.test(str); },
        SafeString: "this.IsSafe(value)",

    };

    /**!
    /* @name 获取随机数 
    /* Jx.random(0,9);
    /* @param &min #最小值
    /* @param &max #最大值
    /* @return {number}
    !*/
    Jx.random = function (min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    };

    /**!
    /* @name HTML常用编码转换 
    /* console.log(Jx.encoding.ToASCII("宋体"));
    /* console.log(Jx.encoding.ToUnicode("宋体"));
    /* console.log(Jx.encoding.ToNormal("宋\\u4F53"));
    /* console.log(Jx.encoding.ToUnicode("宋体"));
    /* console.log(Jx.encoding.ToCss('Arial , Helvetica ,"宋体", sans-serif'));
    /* @return {object}
    !*/
    Jx.encoding = function () {
        function ToASCII(str) {
            return this.ToNormal(str).replace(/[^\u0000-\u00FF]/g, function () { return escape(arguments[0]).replace(/(?:%u)([0-9a-f]{4})/gi, "\$1;") });
        }
        function ToUnicode(str) {
            return this.ToNormal(str).replace(/[^\u0000-\u00FF]/g, function () { return escape(arguments[0]).replace(/(?:%u)([0-9a-f]{4})/gi, "\\u$1") });
        }
        function ToNormal(str) {
            return str.replace(/(?:)([0-9a-f]{4});|(?:\\u)([0-9a-f]{4})/gi, function () { return unescape("%u" + (arguments[1] || arguments[2])); });
        }
        function ToCss(str) {
            return this.ToNormal(str).replace(/[^\u0000-\u00FF]/g, function () { return escape(arguments[0]).replace(/(?:%u)([0-9a-f]{4})/gi, "\\$1") });
        }
        return {
            ToASCII: ToASCII,
            ToUnicode: ToUnicode,
            ToNormal: ToNormal,
            ToCss: ToCss
        };
    }();

    Jx.linq = function (data, func) {
        return new Linq(data,func);
    };



})(window, undefined);